package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.dao.UserNoteMapper;
import mobi.hami.huapin.model.UserNoteLog;
import mobi.hami.huapin.model.UserWechat;
import mobi.hami.huapin.service.UserNoteLogService;
import mobi.hami.huapin.service.UserNoteService;
import mobi.hami.huapin.service.UserWechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Create by 小峰 on 2018/9/13.
 * 用户可用纸条信息
 */
@Service
public class UserNoteServiceImpl implements UserNoteService {

    @Autowired
    private UserNoteLogService userNoteLogService;
    @Autowired
    private UserWechatService userWechatService;
    @Autowired
    private UserNoteMapper userNoteMapper;

    /**
     * 获取纸条入口信息
     * @param spaceId
     * @return
     */
    @Override
    public Map<String, Object> getNoteEnterBySpaceId(Integer spaceId) {
        List<UserNoteLog> userNoteLogList = userNoteLogService.getCurrentDayUserBySpaceId(spaceId);
        int currentNoteCount=0;
        int currentUserCount=0;
        List<String> headPhots=null;

        if(userNoteLogList!=null && userNoteLogList.size()>0)
        {
            //当前纸条数
            currentNoteCount = userNoteLogList.size();
            Map<Integer, List<UserNoteLog>> groupMap = userNoteLogList.stream().collect(Collectors.groupingBy(c -> c.getTo_user_id()));

            currentUserCount = groupMap.size();

            List<Integer> userIds = groupMap.keySet().stream().limit(5).collect(Collectors.toList());

            List<UserWechat> userWechatList = userWechatService.findModelByUserIdArray(userIds);

            headPhots = userWechatList.stream().map(m -> m.getHead_photo()).collect(Collectors.toList());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("NoteCount", currentNoteCount);
        map.put("UserCount", currentUserCount);
        map.put("UserPhotos", headPhots);
        return map;
    }

    /**
     * 根据空间Id获取昨天新增的被系统的贴条信息
     * @param spaceId
     * @return
     */
    @Override
    public Integer getYesterDayNewUserBySpaceId(Integer spaceId) {
        List<UserNoteLog> userNoteLogList=userNoteLogService.getYesterdayNewUserBySpaceId(spaceId);
        if(userNoteLogList!=null && userNoteLogList.size()>0){
            Map<Integer,List<UserNoteLog>> groupMap=userNoteLogList.stream().collect(Collectors.groupingBy(m->m.getTo_user_id()));
            return  groupMap.size();
        }
        return 0;
    }

    /**
     * 获取用户可用的纸条数量
     * @param spaceId
     * @param userId
     * @return
     */
    public Integer getUserNoteCount(Integer spaceId,Integer userId){
        return userNoteMapper.getUserNoteCount(spaceId,userId);
    }

    /**
     * 用户签收后增加纸条数量
     * @param orderId
     * @param spaceId
     * @param userId
     * @return
     */
    public Integer addOrderUserNoteInfo(Integer orderId,Integer spaceId,Integer userId){
        return userNoteMapper.addOrderUserNoteInfo(orderId,spaceId,userId);
    }
}

